package net.web
{
	import flash.utils.getQualifiedClassName;
	
	import net.web.service.WebService;
	
	import struct.HashMap;
	

	public class MCommand
	{
		/** 数据请求中 */		
		private static var requestInsg:HashMap = new HashMap();
		/** 是否是单通道*/
		private var _isSingle:Boolean;
		
		protected var autoConnect:Boolean = true;
		
		protected var connected:Boolean = false;
		
		/**
		 * 请求服务
		 * @param webDestination 服务名
		 * @param methodName 方法名
		 * @param parameters 参数
		 * 
		 */			
		protected function requestWebService(webDestination:String,methodName:String,...parameters):void
		{
			if(autoConnect)
			{
//				WebService.instance.connect(UrlConfig.webServiceUrl);
				connected = true;
			}
			
			if(connected == false)
				throw new Error("通信尚未连接");
			
			if(isRequestIng==true)
				return ;
			
			if(_isSingle==false)
				isSingle = true;
			
			isRequestIng = true;
//			WebService.instance.extMethodArray(webDestination,methodName,rHandler,fHandler,parameters);
		}
		
		/**连接*/
		protected function connect(gateway:String):void
		{
//			WebService.instance.connect(gateway);
			connected = true;
		}
		
		/**
		 * 服务器返回结果的处理 
		 * @param info
		 */
		private function rHandler(info:Object):void
		{
			isRequestIng = false;
			
			resultHandler(info);
		}
		
		/**
		 * 异常的处理
		 * @param info
		 */		
		private function fHandler(info:Object):void
		{
			isRequestIng  = false;
			faultHandler(info);
		}
		
		
		/**
		 * 服务器返回结果的处理 
		 * @param info
		 */
		protected function resultHandler(info:Object):void
		{
			isRequestIng  = false;
		}
		
		/**
		 * 异常的处理
		 * @param info
		 */		
		protected function faultHandler(info:Object):void
		{
			isRequestIng  = false;
		}
		
		/**
		 * 是否是单通道
		 */
		protected function set isSingle(value:Boolean):void
		{
			_isSingle =  value;
			
			if(_isSingle)
			{
				var className:String = getQualifiedClassName(this);
				if(requestInsg.getValue(className)==null)
					requestInsg.put(className,false);
			}
		}
		
		/**
		 * 是否数据请求中
		 */		
		protected function set isRequestIng(value:Boolean):void
		{
			if(_isSingle)
			{
				var className:String = getQualifiedClassName(this);
				requestInsg.put(className,value);
			}
		}
		
		/**
		 * 是否数据请求中
		 */		
		protected function get isRequestIng():Boolean
		{
			if(_isSingle==false)
				return false;
			
			var className:String = getQualifiedClassName(this);
			return requestInsg.getValue(className);
		}
		
	}
}